package controllers

import (
	"WORKSPACE/models"
	"WORKSPACE/pkg/app"
	"WORKSPACE/pkg/e"
	"WORKSPACE/services"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func GetOrders(c *gin.Context){
	err, info, total := services.GetAllorders()
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total},"查询成功")
}

func DelOrders(c *gin.Context){
	id := c.PostForm("orderId")

	fmt.Println(id)
	if id == ""{
		app.INFO(c,30001,"参数错误")
		return
	}
	err := services.Deldetails(id)
	services.Delorders(id)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"删除成功")
}

func UpdateOrders(c *gin.Context){
	orderId := c.PostForm("orderId")
	consumerId := c.PostForm("consumerId")
	tempConsumerId := com.StrTo(consumerId).MustInt()
	goodsId := c.PostForm("goodsId")
	orderDate := c.PostForm("orderDate")
	transRequest := c.PostForm("transRequest")
	isAvailable := c.PostForm("isAvailable")
	transFare := c.PostForm("transFare")
	tempTransFare := com.StrTo(transFare).MustInt()
	if orderId == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}

	err := services.Updateorders(models.Orders{Order_id: orderId, Consumer_id: tempConsumerId, Goods_id: goodsId, Order_date: orderDate, Trans_request: transRequest, Is_available: isAvailable, Trans_fare: tempTransFare})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"更新成功！")
}

func AddOrders(c *gin.Context){
	orderId := c.PostForm("orderId")
	consumerId := c.PostForm("consumerId")
	tempConsumerId := com.StrTo(consumerId).MustInt()
	goodsId:=c.PostForm("goodsId")
	orderDate:= c.PostForm("orderDate")
	transRequest := c.PostForm("transRequest")
	isAvailable := c.PostForm("isAvailable")
	transFare := c.PostForm("transFare")
	tempTransFare := com.StrTo(transFare).MustInt()
	if orderId == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}

	err := services.Addorders(models.Orders{Order_id: orderId, Consumer_id: tempConsumerId, Goods_id: goodsId, Order_date: orderDate, Trans_request: transRequest, Is_available: isAvailable, Trans_fare: tempTransFare})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"添加成功！")
}